#ifndef _MQTT_MESSAGE_DEFINE_H_
#define _MQTT_MESSAGE_DEFINE_H_

#include "Define.h"

#ifdef __cplusplus
extern "C" {
#endif


/**
 *
 * MQTT3.1 Fixed Header
 * | Bit       |          7 ~ 4           |            3 ~ 0            |
 * | byte 1    | MQTT Control Packet Type | Flags specific to each Type |
 * | byte 2... |             Remaining Length(1 ~ 4 bytes)              |
 *
 * MQTT3.1 UTF-8 Encoded Strings
 * | Bit       |                         7 ~ 0                          |
 * | byte 1    |                   String Length MSB                    |
 * | byte 2    |                   String Length LSB                    |
 * | byte 3... |              UTF-8 Encoded Character Data              |
 *
 */

#define MQTT_FIXED_HEADER_LEN                  (1)
#define MQTT_REMAINLEN_MAXLEN                  (4)
#define MQTT_UTF8_STR_EXTRA_LEN                (2)
#define MQTT_PACKETID_LEN                      (2)
#define MQTT_REQUEST_QOS_LEN                   (1)
#define MQTT_QOS0                              (0x00)
#define MQTT_QOS1                              (0x01)
#define MQTT_QOS_MAX                           (1)
#define MQTT_TOPIC_MAXLEN                      (128)
#define MQTT_PAYLOAD_MAXLEN                    (1024 * 1024 + 1)


/** MQTT 3.1 Connect Packet */
#define MQTT_CONN_PKT_TYPE                     (0x10)
#define MQTT_CONN_FIXED_HEADER_LEN             (MQTT_FIXED_HEADER_LEN)     /** value: 0x10 */
#define MQTT_CONN_REMAINLEN_MAXLEN             (MQTT_REMAINLEN_MAXLEN)
#define MQTT_CONN_PROTOCOL_NAME_LEN            (6) /** value: 0x00, 0x04, 0x4D, 0x51, 0x54, 0x54 */
#define MQTT_CONN_PROTOCOL_LEVEL_LEN           (1) /** value: 0x04 */
#define MQTT_CONN_CONNECT_FLAG_LEN             (1) /** value: 0xC0 */
#define MQTT_CONN_KEEP_ALIVE_LEN               (2) /** value: MQTT_conn_pkt_t.keep_alive */
#define MQTT_CONN_FIXED_HEADER_TOTAL_LEN       (MQTT_CONN_FIXED_HEADER_LEN + \
        MQTT_CONN_REMAINLEN_MAXLEN + \
        MQTT_CONN_PROTOCOL_NAME_LEN + \
        MQTT_CONN_PROTOCOL_LEVEL_LEN + \
        MQTT_CONN_CONNECT_FLAG_LEN + \
        MQTT_CONN_KEEP_ALIVE_LEN)
#define MQTT_CONN_REMAINLEN_FIXED_LEN          (MQTT_CONN_PROTOCOL_NAME_LEN + \
        MQTT_CONN_PROTOCOL_LEVEL_LEN + \
        MQTT_CONN_CONNECT_FLAG_LEN + \
        MQTT_CONN_KEEP_ALIVE_LEN)

/** MQTT 3.1 Connect ACK Packet */
#define MQTT_CONNACK_PKT_TYPE                  (0x20)
#define MQTT_CONNACK_FIXED_HEADER_LEN          (MQTT_FIXED_HEADER_LEN) /** value: 0x20 */
#define MQTT_CONNACK_REMAINLEN_MAXLEN          (1) /** value: 0x02 */
#define MQTT_CONNACK_FLAGS_LEN                 (1) /** value: 0x00 */
#define MQTT_CONNACK_RETURN_CODE_LEN           (1)
#define MQTT_CONNACK_FIXED_HEADER_TOTAL_LEN    (MQTT_CONNACK_FIXED_HEADER_LEN + \
                                        MQTT_CONNACK_REMAINLEN_MAXLEN + \
                                        MQTT_CONNACK_FLAGS_LEN + MQTT_CONNACK_RETURN_CODE_LEN)

#define MQTT_CONNACK_RCODE_ACCEPTED                            (0x00)

/** conack error code */
#define MQTT_V5_CONNACK_RCODE_UNACCEPTABLE_PROTOCOL_VERSION    (0x84)
#define MQTT_V5_CONNACK_RCODE_BAD_USERNAME_PASSWORD            (0x86)
#define MQTT_V5_CONNACK_RCODE_SERVER_UNAVAILABLE               (0x88)
#define MQTT_V5_CONNACK_RCODE_NOT_AUTHORIZED                   (0x87)

/** conack error code */
#define MQTT_V5_PROPERTY_ID_LEN                                 (1)
#define MQTT_V5_USER_PROPERTY_KEY_LEN                           (2)
#define MQTT_V5_USER_PROPERTY_VALUE_LEN                         (2)
#define MQTT_V5_TOPIC_ALIAS_LEN                                 (2)
#define MQTT_V5_TOPIC_ALIAS_MAX_LEN                             (2)
#define MQTT_V5_RECEIVE_MAX_LEN                                 (2)
#define MQTT_V5_RESPONSE_TOPIC_LEN                              (2)
#define MQTT_V5_CORELATION_DATA_LEN                             (2)
#define MQTT_V5_REASON_STRING_LEN                               (2)
#define MQTT_V5_DISCONNECT_REASON_CODE_LEN                      (1)

/** disconnect Packet */
#define MQTT_DISCONNECT_PKT_TYPE               (0xE0)

/** ping Request Packet */
#define MQTT_PINGREQ_PKT_TYPE                  (0xC0)

/** MQTT 3.1 Ping Response Packet */
#define MQTT_PINGRESP_PKT_TYPE                 (0xD0)

/** MQTT 3.1 Publish Packet */
#define MQTT_PUBLISH_PKT_TYPE                  (0x30)
#define MQTT_PUBLISH_TOPICLEN_LEN              (2)

/** MQTT 3.1 Publish ACK Packet */
#define MQTT_PUBACK_PKT_TYPE                   (0x40)

/** MQTT 3.1 Subscribe Packet */
#define MQTT_SUB_PKT_TYPE                      (0x80)
#define MQTT_SUB_PKT_RESERVE                   (0x02)
#define MQTT_UNSUB_PKT_RESERVE                 (0x02)

/** MQTT 3.1 Subscribe ACK Packet */
#define MQTT_SUBACK_PKT_TYPE                   (0x90)

#define MQTT_SUBACK_RCODE_MAXQOS0              (0x00)
#define MQTT_SUBACK_RCODE_MAXQOS1              (0x01)
#define MQTT_SUBACK_RCODE_MAXQOS2              (0x02)
#define MQTT_SUBACK_RCODE_FAILURE              (0x80)

/** MQTT 3.1 Unsubscribe Packet */
#define MQTT_UNSUB_PKT_TYPE                    (0xA0)

/** MQTT 3.1 Unsubscribe ACK Packet */
#define MQTT_UNSUBACK_PKT_TYPE                 (0xB0)

/** MQTT 3.1 unimplemented Packet */
#define MQTT_PUBREC_PKT_TYPE                   (0x50)
#define MQTT_PUBREL_PKT_TYPE                   (0x60)
#define MQTT_PUBCOMP_PKT_TYPE                  (0x70)

/** MQTT 5.0 implemented Packet */
#define MQTT_SERVER_DISCONNECT_PKT_TYPE        (0xE0)


/**
 * @brief MQTT5.0 属性的枚举
 *
 * @details
 *
 * 传入@ref conn_property_t 等数据结构的MQTT报文类型
 *
 */
/**
 * @brief
 */
typedef enum {
    MQTTPROP_UNRESOLVED = 0x00,
    MQTTPROP_PAYLOAD_FORMAT_INDICATOR = 0x01,
    MQTTPROP_PUBLICATION_EXPIRY_INTERVAL = 0x2,
    MQTTPROP_CONTENT_TYPE = 0x3,
    MQTTPROP_RESPONSE_TOPIC = 0x8,
    MQTTPROP_CORRELATION_DATA = 0x9,
    MQTTPROP_SUBSCRIPTION_IDENTIFIER = 0xB,
    MQTTPROP_SESSION_EXPIRY_INTERVAL = 0x11,
    MQTTPROP_ASSIGNED_CLIENT_IDENTIFIER = 0x12,
    MQTTPROP_SERVER_KEEP_ALIVE = 0x13,
    MQTTPROP_AUTHENTICATION_METHOD = 0x15,
    MQTTPROP_AUTHENTICATION_DATA = 0x16,
    MQTTPROP_REQUEST_PROBLEM_INFORMATION = 0x17,
    MQTTPROP_WILL_DELAY_INTERVAL = 0x18,
    MQTTPROP_REQUEST_RESPONSE_INFORMATION = 0x19,
    MQTTPROP_RESPONSE_INFORMATION = 0x1A,
    MQTTPROP_SERVER_REFERENCE = 0x1C,
    MQTTPROP_REASON_STRING = 0x1F,
    MQTTPROP_RECEIVE_MAXIMUM = 0x21,
    MQTTPROP_TOPIC_ALIAS_MAX = 0x22,
    MQTTPROP_TOPIC_ALIAS = 0x23,
    MQTTPROP_MAX_QOS = 0x24,
    MQTTPROP_RETAIN_AVAILABLE = 0x25,
    MQTTPROP_USER_PROPERTY = 0x26,
    MQTTPROP_MAX_PACK_SIZE = 0x27,
    MQTTPROP_WILDCARD_SUBSCRIPTION_AVAILABLE = 0x28,
    MQTTPROP_SUBSCRIPTION_IDENTIFIER_AVAILABLE = 0x29,
    MQTTPROP_SHARED_SUBSCRIPTION_AVAILABLE = 0x2A,
    MQTTPROP_MAX,
} MQTTProperty;

#ifdef __cplusplus
}
#endif

#endif




